package com.mcc.web.controller.system;

import java.util.Date;
import java.util.List;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import com.mcc.common.exception.CustomException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.mcc.common.annotation.Log;
import com.mcc.common.core.controller.BaseController;
import com.mcc.common.core.domain.AjaxResult;
import com.mcc.common.core.page.TableDataInfo;
import com.mcc.common.enums.BusinessType;
import com.mcc.common.utils.SecurityUtils;
import com.mcc.system.domain.SysPost;
import com.mcc.system.service.ISysPostService;

/**
 * 岗位信息操作处理
 *
 * @author gxy
 */
@Api(tags = "岗位")
@ApiSort(14)
@RestController
@RequestMapping("/system/post")
public class SysPostController extends BaseController {

    @Autowired
    private ISysPostService postService;

    @ApiOperation("获取岗位列表")
    @ApiOperationSupport(order = 1)
    @PreAuthorize("@ss.hasPermi('system:post:list')")
    @GetMapping("/list")
    public AjaxResult<TableDataInfo<SysPost>> list(SysPost post) {
        List<SysPost> list = postService.selectPostList(post);
        return getDataTable(list);
    }

    @ApiOperation("获取岗位列表(分页)")
    @ApiOperationSupport(order = 2)
    @PreAuthorize("@ss.hasPermi('system:post:list')")
    @GetMapping("/page")
    public AjaxResult<TableDataInfo<SysPost>> page(SysPost post) {
        startPage();
        List<SysPost> list = postService.selectPostList(post);
        return getDataTable(list);
    }

    @ApiOperation("根据岗位编号获取详细信息")
    @ApiOperationSupport(order = 3)
    @PreAuthorize("@ss.hasPermi('system:post:query')")
    @GetMapping("/get/{postId}")
    public AjaxResult<SysPost> getInfo(@PathVariable Long postId) {
        return AjaxResult.success(postService.selectPostById(postId));
    }

    @ApiOperation("新增岗位")
    @ApiOperationSupport(order = 4)
    @PreAuthorize("@ss.hasPermi('system:post:add')")
    @Log(title = "岗位管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody SysPost post) {
        if (!postService.checkPostNameUnique(post)) {
            throw new CustomException("新增岗位'" + post.getPostName() + "'失败，岗位名称已存在");
        }
        if (!postService.checkPostCodeUnique(post)) {
            throw new CustomException("新增岗位'" + post.getPostName() + "'失败，岗位编码已存在");
        }
        return toAjax(postService.insertPost(post));
    }

    @ApiOperation("修改岗位")
    @ApiOperationSupport(order = 5)
    @PreAuthorize("@ss.hasPermi('system:post:edit')")
    @Log(title = "岗位管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody SysPost post) {
        if (!postService.checkPostNameUnique(post)) {
            throw new CustomException("修改岗位'" + post.getPostName() + "'失败，岗位名称已存在");
        }
        if (!postService.checkPostCodeUnique(post)) {
            throw new CustomException("修改岗位'" + post.getPostName() + "'失败，岗位编码已存在");
        }
        return toAjax(postService.updatePost(post));
    }

    @ApiOperation("删除岗位")
    @ApiOperationSupport(order = 6)
    @PreAuthorize("@ss.hasPermi('system:post:remove')")
    @Log(title = "岗位管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove/{postIds}")
    public AjaxResult remove(@PathVariable Long[] postIds) {
        return toAjax(postService.deletePostByIds(postIds));
    }

}
